using System;

namespace ReverseInteger
{
    public class Solution
    {
        public int Reverse(int x)
        {
            var f = false;
            if (x < 0)
            {
                x = -x;
                f = true;
            }

            var m = 0;

            try
            {
                while (x != 0)
                {
                    m = checked(m * 10 + x % 10);
                    x /= 10;
                }
            }
            catch (OverflowException)
            {
                return 0;
            }


            return f ? -m : m;
        }
    }
}